package cn.com.util;

import java.util.List;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.jsp.JspException;
import javax.servlet.jsp.tagext.TagSupport;

import org.springframework.beans.factory.annotation.Autowired;

import cn.com.api.service.*;
import cn.com.api.entity.*;

/**
 * 权限控制标签
 * 
 * @author scott
 * @date 2015-2-5
 */
public class PermissionTag extends TagSupport {


	
	private String module;// 属性名必须与JSP自定义标签的属性名一样

	private String code;

	public String getModule() {
		return module;
	}

	public void setModule(String module) {
		this.module = module;
	}

	public String getCode() {
		return code==null?"":code;
	}

	public void setCode(String code) {
		this.code = code;
	}

	@Override
	public int doStartTag() throws JspException {
		boolean result = false;
		HttpServletRequest request = (HttpServletRequest) this.pageContext.getRequest();// 通过成员变量获取HttpServletRequest对象
		ApiT_user_admin admin = (ApiT_user_admin) request.getSession().getAttribute("user_admin");// 获取登录到系统的用户
		if (admin != null) {
			if ("1".equals(String.valueOf(admin.getIs_super()))) {// 超级管理员
				result = true;
			} else {  
				//params.put("id", String.valueOf(admin.getUser_id()));
				//params.put("module", this.module);
				//params.put("code", this.code);
				//判断用户 是否有权限
			   String[] action_strs = admin.getAction_list().split(",");
			   // 子菜单权限
			   if(this.code!=null && this.code.length()>0){
				   for(String action_str : action_strs){
					   if(!action_str.trim().equals("") && !this.code.trim().equals("")){
						   String[] cur_action_strs = action_str.split("￥");
						   if(cur_action_strs[1].trim().equalsIgnoreCase(this.code.trim()) ){
							   result = true;
							   break;
						   } 
					   }
				   }
			   }
			   // 目录权限
			   if(this.module!=null && this.module.length()>0  && (this.code==null || this.code.length()==0)){
				   for(String action_str : action_strs){
					   String[] cur_action_strs = action_str.split("￥");
					   if(cur_action_strs[0].trim().equalsIgnoreCase(this.module.trim()) ){
						   result = true;
						   break;
					   } 
				   }
			   }
			}
		}
		return result ? EVAL_BODY_INCLUDE : SKIP_BODY;
	}
}